<?php
/**
 * spsNewsletter Model
 * 
 * @author Satyan Shah <joomlaDesign@modernbridge.com>
 * @link http://www.modernbridge.com
 * @license GNU/GPL
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

class spsNewsletterModelspsNewsletter extends JModel
{
	/* this function adds a subscriber to the spsNewsletter.subscribers table
	the function first checks to ensure the email address isn't already stored 
	in the table */
	
	function addSubscriber($name,$email)
	{       
		// get database handle
		$db=& JFactory::getDBO();
		
		// build query to check if the email address exists in the database
		$query = 'SELECT * FROM #__spsnewsletter_subscribers WHERE #__spsnewsletter_subscribers.email="'.$email.'";';
		$db->setQuery($query);
		$rows=$db->loadObjectList();
		
		// if there was a record in the database with this email address then don't add it again!
		if(sizeof($rows))
		{	   
			return -1; // <- Exit Point, Task Failed.
		}
    
		else
		{
			$targetRow =& $this->getTable('subscriber');
			$rowToBind['name']='';
			$rowToBind['email']=$email;
			$rowToBind['enabled']=0;
			$rowToBind['plainText']=0;

			if (!$targetRow->bind($rowToBind)) {
				return 0; // <- Exit Point, Task Failed.
			}
        
			if(!$targetRow->check()) {
				return 0; // <- Exit Point, Task Failed.
			}
      
			if(!$targetRow->store()){
				return 0; // <- Exit Point. Task Failed.
			}
          
		}

	
		return 1; // <- Exit Point. Task Succeeded
 
	}
  
	/* this functio removes a subscriber from the database based on their subscriber ID
	this method is only called when a user presses the unsubscribe link in a newsletter */
	
	function removeSubscriber($subscriber_id)
	{
		// get database handle
		$db=& JFactory::getDBO();
		$query = 'SELECT * FROM #__spsnewsletter_subscribers WHERE #__spsnewsletter_subscribers.id = '.$subscriber_id.' LIMIT 1;';
		$db->setQuery($query);
		$rows=$db->loadObjectList();
      
		// if there was a record in the database then we can delete it
		if(sizeof($rows))
		{
			$row =& $this->getTable('subscriber');
			// delete the subscriber
			if(!$row->delete($subscriber_id))
			{
				$this->setError( $row->getErrorMsg() );
				return false; // Exit Point. Task Failed 
			}

		}
		else
		{	
			// if we are in here it means the subscriber wasn't found in the database
			return false; // <- Exit Point. Task Failed.
		}

		return true; // <- Exit Point. Task Succeded.
	}
    
	/* this functon removes a subscriber from the database based on their subscriber email address
	this method is called when a subscriber uses a form on the website to unsubscribe */
	function removeSubscriberByEmail($email)
	{
		// get database handle
		$db=& JFactory::getDBO();
		$query = 'SELECT * FROM #__spsnewsletter_subscribers WHERE #__spsnewsletter_subscribers.email = "'.$email.'" LIMIT 1;';
		$db->setQuery($query);
		$rows=$db->loadObjectList();
		
		// if there was a record in the database then we can delete it
		if(sizeof($rows))
		{
      			$row =& $this->getTable('subscriber');
			// delete the subscriber
			if(!$row->delete($rows[0]->id))
			{
				$this->setError( $row->getErrorMsg() );
				return false; // Exit Point. Task Failed. 
			}

		}
		else
		{	
			// if we are in here it means the subscriber wasn't found in the database
			return false; // <-Exit Point. Task Failed.
		}

		return true; // <-Exit Point. Task Succeded
	}


}